<template>
  <el-dialog v-model="centerDialogVisible" :title="title" width="40%" align-center >
    <span >请输入类目名称：</span>
    <el-input v-model="input" placeholder="请输入内容" ></el-input>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submithandle">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, toRefs,getCurrentInstance } from 'vue'
//全局导入
const cxt = getCurrentInstance()
const API = cxt.appContext.config.globalProperties.$API
const props = defineProps(['title', 'type', 'cid','id'])
const emits = defineEmits(['update_data'])
const { title,type,cid,id }=toRefs(props)
const centerDialogVisible = ref(false)
const input = ref('')
const submithandle = () => {
  if (type.value == 1) {
    //增加
    insertItemCategory({ cid: cid.value, name: input.value })
  } else if(type.value==2){
    //修改
    updateCategory({ id: id.value, name: input.value })
  } else {
    insertCategory({ name: input.value })
  }
}
//新增子类目录名称 cid name
const insertItemCategory = async(params) => {
  const result = await API.insertItemCategory(params)
  // console.log(result);
  if (result.status == 200) {
    centerDialogVisible.value = false
    emits('update_data')
  }
}
//修改类目 id name
const updateCategory = async(params) => {
  const result = await API.updateCategory(params)
  // console.log(result);
  if (result.status == 200) {
    centerDialogVisible.value = false
    emits('update_data')
  }
}
//新增一级导航类目
const insertCategory = async(params) => {
  const result = await API.insertCategory(params)
  // console.log(result);
  if (result.status == 200) {
    centerDialogVisible.value = false
    emits('update_data')
  }
}
defineExpose({
  centerDialogVisible,
  input
});
</script>

<style lang="less" scoped>

</style>